const db = require('../../../libs/MyDB')

module.exports = {

    //banner
    bannerlist: async function () {
        let result = {}
        await db.multiSelect('study_course', ['course_id', 'cimg'], 'type_id = 5')
            .then(
                res => {
                    result = res
                }
            )
        return result
    },

    //新上好课
    newcourse: async function () {
        let result = {}
        await db.multiSelect('study_course', ['course_id', 'ctitle', 'signup_number', 'cimg', 'ctype'], 'type_id = 1')
            .then(
                res => {
                    result = res
                }
            )
        return result
    },

    //前端课程
    frontendcourse: async function () {
        let result = {}
        await db.multiSelect('study_course', ['course_id', 'ctitle', 'signup_number', 'cimg', 'ctype'], 'type_id = 2')
            .then(
                res => {
                    result = res
                }
            )
        return result
    },

    //后端课程
    rearendcourse: async function () {
        let result = {}
        await db.multiSelect('study_course', ['course_id', 'ctitle', 'signup_number', 'cimg', 'ctype'], 'type_id = 3')
            .then(
                res => {
                    result = res
                }
            )
        return result
    },

    //学习路线
    linecourse: async function () {
        let result = {}
        await db.multiSelect('study_course', ['course_id', 'ctitle', 'signup_number', 'cimg', 'ctype'], 'type_id = 4')
            .then(
                res => {
                    result = res
                }
            )
        return result
    },

    // 初级课程
    primary: async function () {
        return await db.multiSelect('study_course', ['course_id', 'ctitle', 'signup_number', 'cimg', 'ctype'], 'ctype = "初级"')
    },
    // 中级课程
    middlelevel: async function () {
        return await db.multiSelect('study_course', ['course_id', 'ctitle', 'signup_number', 'cimg', 'ctype'], 'ctype = "中级"')
    },
    // 高级课程
    highranking: async function () {
        return await db.multiSelect('study_course', ['course_id', 'ctitle', 'signup_number', 'cimg', 'ctype'], 'ctype = "高级"')
    },

    //我的选课 存课程id和用户id至study-myclass表
    addcourseselection: async function (courseid, userid) {
        // 获取数据库进行比对  如果课程id 和用户id 相等则不进行存储
        let historicaldata = await db.multiSelect('study_myclass', ['course_id', 'user_detail_id'], 'mcid != ""')
        let historobj = JSON.parse(JSON.stringify(historicaldata));
        //遍历历史数据
        for (let i in historobj) {
            //判断用户是否存在
            if (historobj[i].user_detail_id == userid) {
                //判断课程是否已经存在 存在不进行添加
                if (historobj[i].course_id == courseid) {
                    return
                }
            }
        }
        //对应的用户不同的课程进行添加
        return await db.add('study_myclass', {
            course_id: courseid,
            user_detail_id: userid
        })
    },

    //拿用户id获取对应用户 => 我的课程 
    courseselection: async function (userid) {
        return await db.multiSelect('study_classinfo', ['course_id', 'ctitle', 'signup_number', 'cimg', 'ctype'], `user_detail_id = ${userid}`)
    },

    //我的课程
    mycourse: async function (userid) {
        return await db.multiSelect('study_teacher_course', ['teachercoure_id', 'tctitle', 'tcsignup_number', 'tcimg', 'tctype'], `user_detail_id = ${userid}`)
    },


    //我的作业
    mywork: async function (userid) {

        //引入时间插件
        const date = require("silly-datetime");
        //获取作业时间
        let result = await db.multiSelect('study_workinfo', '*', `user_detail_id = ${userid}`)
        //将时间进行转换
        let resulttimes = JSON.parse(JSON.stringify(result))
        for (let i in resulttimes) {
            resulttimes[i].release_time = date.format(resulttimes[i].release_time, 'YYYY-MM-DD');
        }
        
        return resulttimes; 
    }
}